#!/usr/bin/perl -w
=head1 NAME

 install.pl

=head1 SYNOPSIS

 Setup script for dust

=head1 DESCRIPTION

 Safe to run multiple times; later versions of this script will
 remove obsolete directories or files and not touch required #
 directories or files.

 Portions copied from 'apt-cacher'.

=cut

umask 0022;

#############################################################################
### configuration ###########################################################
# Include the library for the config file parser
require '/usr/share/dust/dust-lib.pl';

# Read in the config file and set the necessary variables
my $config_file = '/etc/dust/dust.conf';
my $config_ref;
eval {
        $config_ref = read_config($config_file);
};
my %config = %$config_ref;

# not sure what to do if we can't read the config file...
die "Could not read config file: $@" if $@;

die "Warning: config file could not be parsed ($config_file) / (store_dir is not set)\n" if ($config{store_dir} eq '');

@info=getpwnam("www-data");
my @permcmd;
if (-e $config{store_dir}) {
    @permcmd = ("chown", "--reference", $config{store_dir});
}
elsif (@info) {
    print "Assuming www-data is the user ID used to run dust\n";
    @permcmd = ("chown", "$info[2]:$info[3]");
}
else {
   @permcmd = ("/bin/echo", 
	       "User account for dust/http daemon unknown, please set ownership for the following files manually:");
}

foreach my $dir ($config{store_dir}, $config{log_dir}) {
    if (!-d $dir) {
	print "Doing mkdir($dir, 0755)\n";
	mkdir($dir, 0755);
	system (@permcmd, $dir);
    }
    if (! -w $dir) {
	die "Warning, $dir exists but is not is not writeable for dust!\n";
    }
}

# We used to tack a line onto the end of apache.conf. 
# Now we just symlink into conf.d
if(-d "/etc/apache/conf.d" ){
    symlink("/etc/dust/apache.conf","/etc/apache/conf.d/dust");
}

if(-d "/etc/apache-ssl/conf.d" ){
    symlink("/etc/dust/apache.conf","/etc/apache-ssl/conf.d/dust");
}

if(-d "/etc/apache2/conf.d" ){
    symlink("/etc/dust/apache2.conf",
	    "/etc/apache2/conf.d/dust.conf");
}

# Apache2 needs the cgi module installed, which it isn't by default.
if(-d "/etc/apache2/mods-enabled"){
    symlink("/etc/apache2/mods-available/cgi.load",
	    "/etc/apache2/mods-enabled/cgi.load");
}

exit(0);
